package ch.vd.dec.selt.petale

import grails.converters.JSON

class TypologiePrestationController {

    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : grailsApplication.config.petale.list.offset.toInteger(),  grailsApplication.config.petale.list.max.toInteger())
        [typologiePrestationInstanceList: TypologiePrestation.list(params), typologiePrestationInstanceTotal: TypologiePrestation.count()]
    }

    def create = {
        def typologiePrestationInstance = new TypologiePrestation()
        typologiePrestationInstance.properties = params
        return [typologiePrestationInstance: typologiePrestationInstance]
    }

    def save = {
        def typologiePrestationInstance = new TypologiePrestation(params)
        if (!typologiePrestationInstance.hasErrors() && typologiePrestationInstance.save()) {
            flash.message = "typologiePrestation.created"
            flash.args = [typologiePrestationInstance.id]
            flash.defaultMessage = "TypologiePrestation ${typologiePrestationInstance.id} created"
            redirect(action: "show", id: typologiePrestationInstance.id)
        }
        else {
            render(view: "create", model: [typologiePrestationInstance: typologiePrestationInstance])
        }
    }

    def show = {
        def typologiePrestationInstance = TypologiePrestation.get(params.id)
        if (!typologiePrestationInstance) {
            flash.message = "typologiePrestation.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "TypologiePrestation not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [typologiePrestationInstance: typologiePrestationInstance]
        }
    }

    def edit = {
        def typologiePrestationInstance = TypologiePrestation.get(params.id)
        if (!typologiePrestationInstance) {
            flash.message = "typologiePrestation.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "TypologiePrestation not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [typologiePrestationInstance: typologiePrestationInstance]
        }
    }

    def update = {
        def typologiePrestationInstance = TypologiePrestation.get(params.id)
        if (typologiePrestationInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (typologiePrestationInstance.version > version) {
                    
                    typologiePrestationInstance.errors.rejectValue("version", "typologiePrestation.optimistic.locking.failure", "Another user has updated this TypologiePrestation while you were editing")
                    render(view: "edit", model: [typologiePrestationInstance: typologiePrestationInstance])
                    return
                }
            }
            typologiePrestationInstance.properties = params
            if (!typologiePrestationInstance.hasErrors() && typologiePrestationInstance.save()) {
                flash.message = "typologiePrestation.updated"
                flash.args = [params.id]
                flash.defaultMessage = "TypologiePrestation ${params.id} updated"
                redirect(action: "show", id: typologiePrestationInstance.id)
            }
            else {
                render(view: "edit", model: [typologiePrestationInstance: typologiePrestationInstance])
            }
        }
        else {
            flash.message = "typologiePrestation.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "TypologiePrestation not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
        def typologiePrestationInstance = TypologiePrestation.get(params.id)
        if (typologiePrestationInstance) {
            try {
                typologiePrestationInstance.delete()
                flash.message = "typologiePrestation.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "TypologiePrestation ${params.id} deleted"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "typologiePrestation.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "TypologiePrestation ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "typologiePrestation.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "TypologiePrestation not found with id ${params.id}"
            redirect(action: "list")
        }
    }
	
	def getTypologie(String fieldSort) {
		def entries = TypologiePrestation.createCriteria().list {
				(params."base.id" != null & params."base.id"!="" & params."base.id"!="null") ? base{eq('id', params."base.id".toLong())} : true
				(params."aut.id" != null & params."aut.id" !="" & params."aut.id" !="null") ? aut{eq('id', params."aut.id".toLong())} : true
				(params."cat.id" != null & params."cat.id" !="" & params."cat.id" !="null") ? cat{eq('id', params."cat.id".toLong())} : true
				order(fieldSort, "asc")
			}
	}
		
	def ajaxGetGroupe = {
		def entries = getTypologie("grp").grp.unique()
		
		render entries as JSON
	}
	
}
